﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.Account;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.Account;
using ChiDao.Business.Data.Repositories.Security;
using ChiDao.Business.Models.Security;
using System.Reflection;
using ChiDao.Business.Service.AttachAttribute;

namespace ChiDao.Business.Service.Impl
{
    /// <summary>
    ///     Action模块站点业务实现
    /// </summary>
    [Export(typeof(IActionInfoService))]
    internal class ActionInfoService : IActionInfoService  //注意此处class为internal，不直接供外部初始化
    {
        #region 属性

        #region 受保护的属性

        /// <summary>
        /// 获取或设置 用户信息数据访问对象
        /// </summary>
        [Import]
        protected IActionInfoRepository ActionInfoRepository { get; set; }

        #endregion

        #region 公共属性

        /// <summary>
        /// 获取 用户信息查询数据集
        /// </summary>
        public IQueryable<ActionInfo> Actions
        {
            get { return ActionInfoRepository.Entities; }
        }

        #endregion

        #endregion

        #region 方法
        /// <summary>
        /// 通过反射取得所有ActionInfo
        /// </summary>
        /// <returns></returns>
        public List<ActionInfo> GetAllActionInfoByAssembly()
        {
            var result = new List<ActionInfo>();
            var types = Assembly.Load("ChiDao.Site.Web").GetTypes();
            foreach (var type in types)
            {
                if (type.BaseType.Name == "BaseController")  //如果继承于BaseController
                {
                    var members = type.GetMethods();
                    foreach (var member in members)
                    {
                        if (member.ReturnType.Name == "ActionResult")  //如果是Action
                        {
                            var newActionInfo = new ActionInfo();

                            object[] WebPageAttrs = member.GetCustomAttributes(typeof(WebPageAttribute), true);  //WebPage特性
                            //如果是页面WebPage，则不加入功能列表
                            if (WebPageAttrs.Length > 0)
                                continue;

                            newActionInfo.ActionName = member.Name;
                            newActionInfo.ControllerName = member.DeclaringType.Name.Substring(0, member.DeclaringType.Name.Length - 10);  //去掉“Controller”后缀
                            object[] descriptionAttrs = member.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);  //描述特性
                            if (descriptionAttrs.Length > 0)
                                newActionInfo.Description = (descriptionAttrs[0] as System.ComponentModel.DescriptionAttribute).Description;  //取得描述（建议每个Controller的Action都写详细描述）
                            result.Add(newActionInfo);
                        }
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 分页取得ActionInfo
        /// </summary>
        /// <param name="query"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<ActionInfo> QueryActionInfoPlist(string query, int start, int limit, out long total)
        {
            List<ActionInfo> allActions = GetAllActionInfoByAssembly();

            total = (from a in allActions
                     where a.ActionName.ToLower().Contains(query.ToLower())
                     select a).Count();

            var result = (from a in allActions
                          where a.ActionName.ToLower().Contains(query.ToLower())
                          select a).Skip(start).Take(limit);

            return new List<ActionInfo>(result);
        }
        #endregion
    }
}